﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcEngine.Core.Services;
using System.Web.Mvc;
using MvcEngine.Mvc.Extensions;
using MvcEngine.Core;
using MvcEngine.Core.Registry;
using MvcEngine.Blog.Filters;
using MvcEngine.Mvc.Filters;
using MvcEngine.Mvc;
using MvcEngine.Blog.Models.Entities;
using MvcEngine.Blog.ModelBinders;
using MvcEngine.Blog.StarterSite.Controllers;
using MvcEngine.Mvc.Services;

namespace MvcEngine.Blog.Services
{
    public class CategoryControlService : ControlServiceBase
    {
        public override void RegisterBackofficeRoutes(AreaRegistrationContext context)
        {
            context.MapMvcEngineRoute(
                "BackOffice_BlogCategoryList",
                "BackOffice/{blogName}/Blog/Categories",
                new { controller = "BlogCategory", action = "List"},
                new string[] { "MvcEngine.Blog.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_BlogCategoryPaging",
                "BackOffice/{blogName}/Blog/Categories/Page{pageNumber}",
                new { controller = "BlogCategory", action = "List", pageNumber = "0" },
                new string[] { "MvcEngine.Blog.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_BlogNewCategory",
                "BackOffice/{blogName}/Blog/Category/New",
                new { controller = "BlogCategory", action = "Edit", id = "" },
                new string[] { "MvcEngine.Blog.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_BlogEditCategory",
                "BackOffice/{blogName}/Blog/Category/Edit/{id}",
                new { controller = "BlogCategory", action = "Edit"},
                new string[] { "MvcEngine.Blog.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_BlogDeleteCategory",
                "BackOffice/{blogName}/Blog/Category/Delete/{id}",
                new { controller = "BlogCategory", action = "Delete"},
                new string[] { "MvcEngine.Blog.BackOffice.Controllers" }
            );

        }


        public override void RegisterActionFilters(IActionFilterRegistry registry)
        {
            //--------------------------------------------------------------------------------------
            //------------------------------Common filters------------------------------------------
            //--------------------------------------------------------------------------------------
            ControllerCriteria backOfficeCommonCriteria = new ControllerCriteria();
            backOfficeCommonCriteria.AddController(typeof(MvcEngine.Blog.BackOffice.Controllers.BlogCategoryController));
            registry.Add(new[] { backOfficeCommonCriteria }, typeof(MvcEngine.Blog.BackOffice.Filters.BlogActionFilter));

            ControllerCriteria starterSiteCommonCriteria = new ControllerCriteria();
            starterSiteCommonCriteria.AddController(typeof(MvcEngine.Blog.StarterSite.Controllers.BlogCategoryController));
            registry.Add(new[] { starterSiteCommonCriteria }, typeof(MvcEngine.Blog.StarterSite.Filters.BlogActionFilter));

            //--------------------------------------------------------------------------------------
            //------------------------------End of common filters-----------------------------------
            //--------------------------------------------------------------------------------------


            ControllerCriteria moduleCriteria = new ControllerCriteria();
            moduleCriteria.AddController(typeof(MvcEngine.Blog.BackOffice.Controllers.BlogCategoryController));
            registry.Add(new[] { moduleCriteria }, new ModuleActionFilter());


            //--------------------------------------------------------------------------------------
            //---------------------------------Permissions------------------------------------------
            //--------------------------------------------------------------------------------------
            ControllerCriteria viewCriteria = new ControllerCriteria();
            viewCriteria.AddController(typeof(MvcEngine.Blog.StarterSite.Controllers.BlogCategoryController));

            ControllerCriteria editCriteria = new ControllerCriteria();
            editCriteria.AddController(typeof(MvcEngine.Blog.BackOffice.Controllers.BlogCategoryController));
            //--------------------------------------------------------------------------------------
            //---------------------------------End of permissions----------------------------------
            //--------------------------------------------------------------------------------------

            registry.Add(new[] { editCriteria }, new RolePermissionAttribute(Constants.Roles.Administrators));
            registry.Add(new[] { viewCriteria }, new ModulePermissionAttribute(PermissionTypes.View));
        }

        public override void RegisterModelBinders(ModelBinderDictionary binders)
        {
            binders[typeof(BlogCategory)] = new BlogCategoryModelBinder();
        }

        public override Type GetControllerType()
        {
            return typeof(BlogCategoryController);
        }

        public override string GetDefaultAction()
        {
            return "Index";
        }
    }
}
